package com.guicelcc.guicedemo.poi;

import java.io.File;
import java.io.FilenameFilter;

import com.guicelcc.guicedemo.poi.WordToPDF.TestThread;

	public class Word2PDFUtil {
	     
	     /**
	      * @param dirs doc转换文件夹，批量转换
	      */
	     /*public static void word2Pdf(File file,String path){
	          File dir = new File(dirs); 
	          File[] files = dir.listFiles(new WordFilenameFilter()); 
	            //遍历文件夹方式
	             if (files == null||files.length==0){
	                 throw new NullPointerException("该路径下没有doc文件");
	             }
	             for (int i = 0; i < files.length; i++) { 
	                     String strFileName = files[i].getAbsolutePath().toLowerCase(); 
	                     TestThread t1 = new WordToPDF.TestThread();
	                     //输入文件名
	                    t1.setInputFile(new File(strFileName));
	                     //获得"."前面的文件名并将其输入为PDF
	                     t1.setOutputFile(new File(strFileName.substring(0,strFileName.indexOf("."))+".pdf"));
	                     t1.start();
	             }
	     
	     } */
		
		
		public static void wordPdfFromPath(String path,String topath) {
			File dir = new File(path); 
	          File[] files = dir.listFiles(new WordFilenameFilter()); 
	            //遍历文件夹方式
	             if (files == null||files.length==0){
	                 throw new NullPointerException("该路径下没有doc文件");
	             }
	             for (int i = 0; i < files.length; i++) { 
	            	 String strFileName = files[i].getAbsolutePath();
	            	 //System.out.println(strFileName);
	            	 wordPdf(strFileName,topath);
	             }
		}
	     
	     public static void wordPdf(String srccpath,String topath){
	          //File file = new File(srccpath); 
	            //遍历文件夹方式
	                    // String strFileName = file.getAbsolutePath().toLowerCase(); 
	                   //  TestThread t1 = new WordToPDF.TestThread();
	                     //输入文件名
	                   // t1.setInputFile(new File(topath));
	                     //获得"."前面的文件名并将其输入为PDF
	                  //   t1.setOutputFile(new File(strFileName.substring(0,strFileName.indexOf("."))+".pdf"));
	                   //  t1.start();
	                      File inputFile= new File(srccpath); 
	                      String strFileName = inputFile.getName();
	                      //System.out.println(strFileName.substring(0,strFileName.indexOf(".")));
	                      File outputFile = new File(topath+strFileName.substring(0,strFileName.indexOf("."))+".pdf");     
	                      WordToPDF t = new WordToPDF();
	                      t.docToPdf(inputFile, outputFile);
	                      System.out.println(outputFile.getName()+"文件已生成");
	    } 
	 
	     /**
	      * @param orgfileName 原始word文件名
	      * @param descFileName 生成pdf文件名
	      */
	     public static void word2Pdf(String orgfileName, String descFileName) {
	         if(!isWord(orgfileName)||descFileName==null){
	             throw new IllegalArgumentException("原始word文件名不是word文档,或者descFileName为空");
	         }
	         TestThread t1 = new WordToPDF.TestThread();
	         // 输入文件名
	         t1.setInputFile(new File(orgfileName));
	         // 获得"."前面的文件名并将其输入为PDF
	         t1.setOutputFile(new File(descFileName.substring(0, descFileName
	                 .indexOf("."))
	                 + ".pdf"));
	         t1.start();
	     }
	     static class WordFilenameFilter implements FilenameFilter{
	         @Override
	         //只转换word文档
	         public boolean accept(File dir, String name) {
	             return isWord(name);
	         }
	         
	     }
	     /**
	      * @param name 文件名
	      * @return 判断是否为doc word文档
	      */
	     private static boolean isWord(String name){
	         return name.endsWith(".doc")||name.endsWith(".docx")||name.endsWith(".wps");
	         //return true;
	     }
	}
